package com.linln.modules.member.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.linln.common.data.PageSort;
import com.linln.modules.member.domain.MemberCollect;
import com.linln.modules.member.repository.MemberCollectRepository;
import com.linln.modules.member.service.MemberCollectService;

/**
 * @author hct
 * @date 2019/09/12
 */
@Service
public class MemberCollectServiceImpl implements MemberCollectService {

    @Autowired
    private MemberCollectRepository memberCollectRepository;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    @Transactional
    public MemberCollect getById(Long id) {
        return memberCollectRepository.findById(id).orElse(null);
    }
   @Override
   public MemberCollect getByMemberIdAndTypeId(Long memberId,Integer type,Long storeId){
	   return memberCollectRepository.getByMemberIdAndTypeId(memberId,type,storeId);
   }
   @Override
    public   Boolean delCollect(Long id){
	   boolean isOk=false;
	   try{
        memberCollectRepository.deleteById(id);
        isOk=true;
	   }catch(Exception e){
		   
	   }
       return isOk;
   }
    
    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<MemberCollect> getPageList(Example<MemberCollect> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return memberCollectRepository.findAll(example, page);
    }

    /**
     * 保存数据
     * @param MemberCollect 实体对象
     */
    @Override
    public MemberCollect save(MemberCollect memberCollect) {
        return memberCollectRepository.save(memberCollect);
    }


    /**
     * 查询我的消息数量
     * @param memberId
     * @return
     */
    public Long countCollect(Long memberId){
        ExampleMatcher matcher = ExampleMatcher.matching();
        MemberCollect memberCollect=new MemberCollect();
        memberCollect.setMemberId(memberId);
        Example<MemberCollect> example = Example.of(memberCollect, matcher);
        return memberCollectRepository.count(example);
    }
}